package com.edz.dudumifi.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.edz.dudumifi.base.Config;
import com.edz.dudumifi.base.MyApplication;
import com.edz.dudumifi.event.KKTcpClientThread;
import com.edz.dudumifi.event.NettyClientConnectionThread;
import com.edz.dudumifi.event.SendMsgThread;
import com.edz.dudumifi.event.SendSystemInfioThread;

public class InitService extends Service {
    private static final String TAG = "InitService";
    private String strReq, strRtn;       //请求、响应

    private LocationClient client;       //客户端定位
    private String address;           //当前地址
    private String province;          //当前省份
    private String city;              //当前市
    private double lat;               //当前位置
    private double lon;

    public InitService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate()");

        //保存imei
        KKTcpClientThread systenInfo = new KKTcpClientThread(MyApplication.getContext(), Config.req_getsysteminfo_localdevice);
        systenInfo.start();

        //获取lbs enable权限
        KKTcpClientThread Lbscrt = new KKTcpClientThread(MyApplication.getContext(), Config.req_lbsctrl_localdevice);
        Lbscrt.start();

        //与服务器连接
        NettyClientConnectionThread connection = new NettyClientConnectionThread(MyApplication.getContext());
        connection.start();

        //获取定位数据
        getLocation();

        //请求重定向配置
        SendMsgThread webRedirect = new SendMsgThread(MyApplication.getContext(), "{\"MsgType\":\"WebRedirectConfig\"}");
        webRedirect.start();

        //请求限速配置
        SendMsgThread netRate = new SendMsgThread(MyApplication.getContext(), "{\"MsgType\":\"SetNetRate\"}");
        netRate.start();

        //定时发送设备信息
        SendSystemInfioThread systemInfo = new SendSystemInfioThread(MyApplication.getContext(), Config.req_getsysteminfo_localdevice);
        systemInfo.start();

    }

    private void getLocation() {
        client = new LocationClient(getApplicationContext());
        //给客户端注册监听
        client.registerLocationListener(new MyLocationLitener());
        //新建一个客户端位置选项
        LocationClientOption option = new LocationClientOption();
        //设置打开GPS
        option.setOpenGps(true);
        //设置地区类型
        option.setAddrType("all");
        option.setCoorType("bd0911");
        //设置范围
        option.setScanSpan(600);
        //客户端得到这个选项
        client.setLocOption(option);
        //打开客户端
        client.start();
    }

    private class MyLocationLitener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null) {
                Toast.makeText(getApplicationContext(), "网络无法连接，定位失败！", Toast.LENGTH_SHORT).show();
                return;
            } else {
                address = bdLocation.getAddrStr();
                province = bdLocation.getProvince();
                city = bdLocation.getCity();
                lat = bdLocation.getLatitude();
                lon = bdLocation.getLongitude();

                sendLbsInfoServer(province, city, address, lon, lat);
                client.stop();
            }
        }

        private void sendLbsInfoServer(String strProvince, String strCity, String strAddress, double str_lon, double str_lat) {
            Log.i(TAG, "当前的位置信息 == " + strProvince + "\n" + strCity + "\n" + strAddress + "\n" + str_lat + "\n" + str_lon);
            strReq = "{\"MsgType\":\"LbsInfo\",\"Province\":\"" + strProvince + "\",\"City\":\"" + strCity + "\",\"Address\":\"" + strAddress + "\",\"Lon\":\"" + str_lon + "\",\"Lat\":\"" + str_lat + "\"}";
            //发送位置信
            SendMsgThread lbsInfo = new SendMsgThread(MyApplication.getContext(), strReq);
            lbsInfo.start();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
